home *** CD-ROM | disk | FTP | other *** search
- /*
- * beep.c
- *
- * Ultra-simple beep, for keyclick
- * MWS, 5/92.
- *
- * Hacked from audio1.c (RKM companion disks)...
- * ...Copyright (c) 1990 Commodore-Amiga, Inc.
- */
- #include <exec/types.h> /* Some header files for system calls */
- #include <exec/memory.h>
- #include <devices/audio.h>
- #include <graphics/gfxbase.h>
- #include <proto/exec.h>
- #include <proto/dos.h>
- #include "beep.h" /* prototypes */
-
- static struct GfxBase *GfxBase;
- static UBYTE whichannel[] = {1, 2, 4, 8};
- static struct IOAudio *AIOptr; /* Pointer to the IO block for IO commands */
- static struct MsgPort *port; /* Pointer to a port so the device can reply */
-
- extern long samples, frequency, duration;
- UBYTE __chip waveptr[] = { 0, 60, 100, 127, 100, 60,
- 0,-60,-100,-127,-100,-60 };
-
- long clock = 3579545; /* default to PAL if no GfxBase */
-
- #define SAMPLES (sizeof(waveptr))
- #define DURATION 15 /* milliseconds */
-
- void
- FreeAudio () /* free allocated audio resources */
- {
- if (AIOptr)
- {
- if (AIOptr->ioa_Request.io_Device)
- CloseDevice ((struct IORequest *) AIOptr);
- FreeMem (AIOptr, sizeof (struct IOAudio));
- AIOptr = NULL;
- }
- if (port)
- {
- DeleteMsgPort (port);
- port = NULL;
- }
- }
-
- BOOL
- AllocAudio () /* allocate audio resources */
- {
- if (GfxBase = (void *)OpenLibrary("graphics.library", 0L))
- {
- if (GfxBase->DisplayFlags & PAL)
- clock = 3456895;
- CloseLibrary(GfxBase);
- }
-
- if ((AIOptr = (void *)AllocMem (sizeof (struct IOAudio), MEMF_PUBLIC | MEMF_CLEAR)) &&
- (port = CreateMsgPort ()))
- {
- AIOptr->ioa_Request.io_Message.mn_ReplyPort = port;
- AIOptr->ioa_Request.io_Message.mn_Node.ln_Pri = 0;
- AIOptr->ioa_Request.io_Command = ADCMD_ALLOCATE;
- AIOptr->ioa_Request.io_Flags = ADIOF_NOWAIT;
- AIOptr->ioa_AllocKey = 85;
- AIOptr->ioa_Data = whichannel;
- AIOptr->ioa_Length = sizeof (whichannel);
-
- /** Open the audio device and allocate a channel **/
- if (!OpenDevice ("audio.device", 0L, (struct IORequest *) AIOptr, 0L))
- return TRUE;
- }
- FreeAudio ();
- return FALSE;
- }
-
- void
- beep (long volume, long frequency)
- {
- static struct Message *msg; /* Pointer for the reply message */
-
- AIOptr->ioa_Request.io_Command = CMD_WRITE;
- AIOptr->ioa_Request.io_Flags = ADIOF_PERVOL;
- AIOptr->ioa_Data = (BYTE *) waveptr;
- AIOptr->ioa_Length = SAMPLES;
- AIOptr->ioa_Period = clock / (SAMPLES * frequency);
- AIOptr->ioa_Cycles = (frequency * DURATION) / 1000;
- AIOptr->ioa_Volume = volume;
-
- BeginIO ((struct IORequest *) AIOptr);
- Wait (1L << port->mp_SigBit);
- msg = GetMsg (port);
- }
-